//
//  FriendManager.m
//  FootPrint
//
//  Created by alex tao on 4/9/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "FriendManager.h"

@implementation FriendManager

static FriendManager * _sharedInstance = nil;

+ (FriendManager*) sharedInstance
{
	if (_sharedInstance == nil) {
        _sharedInstance = [[FriendManager alloc] init];
    }
	return _sharedInstance;
}

- (id)init
{
    self = [super init];
    if (self) {
        _parser = [[FriendParser alloc] init];
        _parser.delegate = self;
        
        _friendList = [[NSMutableArray alloc] initWithCapacity:32];
        _groupList = [[NSMutableArray alloc] initWithCapacity:32];
    }
    return self;
}

- (void)dealloc
{
    [_groupList release];
    [_parser release];
    [_friendList release];
    [super dealloc];
}

- (void)updateAllList
{
    [_parser updateFriendListForUid:[GConfig getUserId]];
    [_parser updateGroupListForUid:[GConfig getUserId]];
}

- (void) removeAll
{
    [_friendList removeAllObjects];
    [_groupList removeAllObjects];
}

- (NSArray*) allFriends
{
    NSMutableArray * retArr = [NSMutableArray arrayWithCapacity:_friendList.count];
    for (id msg in _friendList) {
        [retArr addObject:msg];
    }
    return retArr;
}

- (NSArray*) allGroup
{
    NSMutableArray * retArr = [NSMutableArray arrayWithCapacity:_groupList.count];
    for (id msg in _groupList) {
        [retArr addObject:msg];
    }
    return retArr;
}

- (void) mergeFriendList:(FriendQueryResponseMessage*)msg;
{
    [self didRecieveFriendList:msg];
}

- (void) mergeGroupList:(GroupQueryResponseMessage*)msg
{
    [self didRecieveGroupList:msg];
}

#pragma mark - delegate

- (void) didRecieveFriendList:(FriendQueryResponseMessage*)friends
{
    if (friends && friends.resultCode == 0) {
        [_friendList removeAllObjects];
        PBArray * friendArr = friends.friendList;
        for (int i = 0; i < friendArr.count; i++) {
            [_friendList addObject:[friendArr objectAtIndex:i]];
        }
        _isLatestList = YES;
    }
}

- (void) didRecieveGroupList:(GroupQueryResponseMessage*)groups
{
    if (groups && groups.resultCode == 0) {
        [_groupList removeAllObjects];
        PBArray * groupArr = groups.list;
        for (int i = 0; i < groupArr.count; i++) {
            [_groupList addObject:[groupArr objectAtIndex:i]];
        }
        _isLatestGroup = YES;
    }
}

- (void) didDeleteGroup:(GroupDeleteResponseMessage*)result
{
    
}

@end
